package com.example.lyl.yuejian.view.my;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.adapter.PhotoLvAdapter;
import com.example.lyl.yuejian.bean.PhotoLvBean;
import com.example.lyl.yuejian.net.NetTool;
import com.example.lyl.yuejian.util.SharePreferenceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MyPhotoActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.MyPhotoLv)
    ListView MyPhotoLv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_photo);
        ButterKnife.bind(this);

        toolbar.setTitle("我的晒图");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            //是左上角按键出现
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        final PhotoLvAdapter adapter = new PhotoLvAdapter();
        MyPhotoLv.setAdapter(adapter);
        adapter.setFlag(true);

        NetTool.getInstance().getApi()
                .myPhotoRequest(SharePreferenceUtils.getInt(MyPhotoActivity.this,"uid",0))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<PhotoLvBean>() {
                    @Override
                    public void accept(PhotoLvBean photoLvBean) throws Exception {
                        adapter.setPhotoLvBean(photoLvBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Toast.makeText(MyPhotoActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }
                });


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }
}
